//
//  ATScriptStep.m
//  AutomationTool
//
//  Created by Laurie Marmon on 2/26/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ATScriptStep.h"
#import "ATScriptStepTemplate.h"
#import "ATTemplateManager.h"

#pragma mark Private Properties

@interface ATScriptStep ()

@property (nonatomic, strong) NSMutableDictionary *valuesByParameterName;

@end

#pragma mark Main Class Implementation

@implementation ATScriptStep

@synthesize name = _name;
@synthesize userDescription = _userDescription;
@synthesize stepTemplate = _scriptStepTemplate;
@synthesize valuesByParameterName = _valuesByParameterName;
@synthesize displayString = _displayString;

- (id)init
{
    self = [super init];
    if (self) {
        self.valuesByParameterName = [[NSMutableDictionary alloc] init];
        self.displayString = self.stepTemplate.displayTemplate;
    }
    return self;
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"Name=%@\n ParameterValues=%@", self.name, self.valuesByParameterName];
}

- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:_name forKey:@"ATScriptStepName"];
    [aCoder encodeObject:_userDescription forKey:@"ATScriptStepUserDescription"];
    [aCoder encodeObject:_scriptStepTemplate.name forKey:@"ATScriptStepTemplateName"];
    [aCoder encodeObject:_valuesByParameterName forKey:@"ATScriptStepParameterValues"];
    [aCoder encodeObject:_displayString forKey:@"ATScriptStepDisplayString"];
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];
    _name = [aDecoder decodeObjectForKey:@"ATScriptStepName"];
    _userDescription = [aDecoder decodeObjectForKey:@"ATScriptStepUserDescription"];
    _valuesByParameterName = [aDecoder decodeObjectForKey:@"ATScriptStepParameterValues"];
    _displayString = [aDecoder decodeObjectForKey:@"ATScriptStepDisplayString"];
    
    NSString *templateName = [aDecoder decodeObjectForKey:@"ATScriptStepTemplateName"];
    _scriptStepTemplate = [[ATTemplateManager sharedManager] templateWithName:templateName];
    return self;
}

- (void)setValue:(id)value forParameterName:(NSString *)parameterName
{
    [self.valuesByParameterName setObject:value forKey:parameterName];
    
    self.displayString = self.stepTemplate.displayTemplate;
    
    //now update the displayString shown in the table to show progress
    for (NSString *key in [self parameterNames]) {
        NSString *parameterString = [NSString stringWithFormat:@"%@ =", key];
        NSString *newParameterString = [NSString stringWithFormat:@"%@ = %@", key, [self valueForParameterName:key]];                             
        self.displayString = [self.displayString stringByReplacingOccurrencesOfString:parameterString withString:newParameterString];
    }
}

- (id)valueForParameterName:(NSString *)parameterName
{
    return [self.valuesByParameterName objectForKey:parameterName];
}

- (NSArray *)parameterNames 
{
    return [self.valuesByParameterName allKeys];
}

- (void)setScriptStepTemplate:(ATScriptStepTemplate *)scriptStepTemplate
{
    _scriptStepTemplate = scriptStepTemplate;
    self.displayString = self.stepTemplate.displayTemplate;
    self.userDescription = self.stepTemplate.userDescription;
    self.name = self.stepTemplate.name;
}

@end
